package com.idp.app.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import cn.hutool.core.util.ObjectUtil;
import com.idp.app.dto.*;
import com.idp.app.entity.SyLxrEntity;
import com.idp.app.mapper.SyLxrMapper;
import com.idp.app.service.SyLxrService;
import com.idp.common.enums.MessageEnum;
import com.idp.common.error.SystemException;
import com.idp.common.model.PageModel;
import com.idp.system.mapper.SysDictionaryMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 镇街检察联络表Service实现类
 *
 * @author Simon
 */
@Service("syLxrService")
public class SyLxrServiceImpl implements SyLxrService {

    @Autowired
    private SyLxrMapper syLxrMapper;
    @Autowired
    private SysDictionaryMapper sysDictionaryMapper;

    @Override
    public PageModel<SyLxrDto> findByPage(SyLxrSearchDto syLxrSearchDto) {

        PageModel<SyLxrDto> page = new PageModel<>();
        page.setList(syLxrMapper.getList(syLxrSearchDto));
        return page;
    }

    @Override
    public SyLxrDto getById(Long id) {

        if (ObjectUtil.isEmpty(id)) {
            throw new SystemException(MessageEnum.PARAM_ERROR);
        }

        SyLxrDto syLxrDto = syLxrMapper.getById(id);

        if (syLxrDto == null) {
            throw new SystemException(MessageEnum.DATA_NOT_EXIST);
        }

        return syLxrDto;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void add(SyLxrDto syLxrDto) {

        SyLxrEntity syLxrEntity = BeanUtil.toBean(syLxrDto, SyLxrEntity.class);
        syLxrEntity.setBzName(syLxrDto.getJzName());
        syLxrMapper.insert(syLxrEntity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(SyLxrDto syLxrDto) {

        SyLxrEntity syLxrEntity = syLxrMapper.selectById(syLxrDto.getId());

        if (syLxrEntity == null) {
            throw new SystemException(MessageEnum.DATA_NOT_EXIST);
        }
        CopyOptions copyOptions = new CopyOptions();
        copyOptions.setIgnoreNullValue(true);
        BeanUtil.copyProperties(syLxrDto, syLxrEntity, copyOptions);
        syLxrMapper.updateById(syLxrEntity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(Long id) {

        SyLxrEntity syLxrEntity = syLxrMapper.selectById(id);

        if (syLxrEntity == null) {
            throw new SystemException(MessageEnum.DATA_NOT_EXIST);
        }

        syLxrMapper.deleteById(id);
    }

    @Override
    public List<SyLxrDto> getSyLxrByDqbm(ScreenCommonDto screenCommonDto) {
        return syLxrMapper.getSyLxrByDqbm(screenCommonDto);
    }

}
